#!/usr/bin/perl
# $Id$
#
# Description: Mutt Sigtrace wrapper
#
# Copyright (C) 2005,2006 Charles Mauch <cmauch@gmail.com>
#               2000 Darxus@ChaosReigns.com
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# http://www.ChaosReigns.com/code/mutt-sigtrace/
# http://www.taclug.org/~cmauch/crypto/sigtrace/
#
# -- [ meta information ] ----------------------------------------------------
#
# To use this script, modify your .muttrc so that the following two lines:
#
# set pgp_decode_command="gpg %?p? --passphrase-fd 0? --no-verbose --batch --output - %f"
# set pgp_verify_command="gpg --no-verbose  --batch --output - --verify %s %f"
#
# looks like this:
#
# set pgp_decode_command="~/.mutt/mutt-sigtrace.pl 3276D24897289852 gpg ....
# set pgp_verify_command="~/.mutt/mutt-sigtrace.pl 3276D24897289852 gpg ....
#
# where .... is the rest of the command listed above.
#
# You might also want to scroll down to the bottom of this script and modify
# the $path variable to fit your needs.
#
# Now when mutt sees a pgp signature, it will check the message validity and
# immediately following the signature output, you'll see sigtrace's output.
#
# Neat stuff.
#
# -- [ changelog ] -----------------------------------------------------------
#
#  v0.3 Nov 21 21:29 EDT Initial Creation (Darxus)
#  v0.4 Jul 05 17:28 PST Updated to convert between short key id's which
#                        mutt outputs and long key id's which sigtrace uses.
#

$linenum = 0;

$myid = shift @ARGV;

open( CMD, "@ARGV 2>&1 >/dev/null|" );

while ( $line = <CMD> ) {
    print STDERR "$line";
    if ( $line =~ "key ID" ) {
        $shortid = ( reverse( split( ' ', $line ) ) )[0];
        $convert = `gpg --list-keys --with-colons $shortid | grep -m1 "pub" 2>&1`;
        ( $type, undef, undef, undef, $id, undef, undef, undef, undef, undef, undef, undef, undef, undef ) = split( ':', $convert );

    }
} ## end while ( $line = <CMD> )

if ( $id ) {
    $path = `~/.mutt/scripts/sigtrace.pl $myid $id`;
    if ( $path ) {
        print STDERR "$path\n";
    } else {
        print STDERR "No PGP Key Key path found, PGP Key Tracing Failed!.\n";
    }
} else {
    print STDERR "No destination KeyID found, PGP Key Tracing impossible\n";
}
